package com.example.springbook.controller;
import com.example.springbook.constonts.Constants;
import com.example.springbook.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.springbook.service.BookService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;



@RequestMapping("/book")
@RestController
@Slf4j
public class BookController {
    @Autowired
    private BookService bookService;


    @RequestMapping(value = "/addBook",produces = "application/json")
    public Result addBook(BookInfo bookInfo) {
        if (!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() == null
                || bookInfo.getPrice() == null
                || !StringUtils.hasLength(bookInfo.getPublish())
        ) {
            return Result.fail("参数错误");
        }
        try {
            bookService.addBook(bookInfo);
        } catch (Exception e) {
            log.error("添加图书失败", e);
           Result.fail("内部发生错误请联系管理员");
        }
        return Result.success("");
    }

    @RequestMapping("/getListByPage")
    public Result<PageResult<BookInfo>> getListByPage(PageRequest pageRequest, HttpSession session) {
        log.info("获取图书列表, pageRequest:{}", pageRequest);
    //    UserInfo userinfo =(UserInfo) session.getAttribute(Constants.USER_SESSION_KEY);
    //    if(userinfo==null ||userinfo.getId()<1){
    //        return Result.unlogin();
    //    }
        if (pageRequest.getCurrentPage() < 1) {
            return Result.fail("非法参数");
        }
        PageResult<BookInfo> PageResult = bookService.getListByPage(pageRequest);
        return Result.success(PageResult);
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId) {
        log.info("查询图书信息,bookId:"+bookId);
        if (bookId < 1) {
            log.error("非法图书Id,bookId:" + bookId);
            return null;
        }
      return bookService.queryBookById(bookId);


    }

    @RequestMapping("/updateBook")
    public Result updateBook(BookInfo bookInfo) {
        log.info("更新图书,updateBook:{}", bookInfo);
        if (bookInfo.getId() < 0) {
            return Result.fail(false,"ID不合法");
        }
        try {
            Integer result = bookService.updateBook(bookInfo);
            if (result <= 0) {
                return Result.fail("更新失败");
            }
            return Result.success(true);

        } catch (Exception e) {
            log.error("修改图书失败", e);
            return Result.fail(false,e.getMessage());
        }

    }

    @RequestMapping("/batchDelete")
    public boolean batchDelete(@RequestParam List<Integer> ids) {
        log.info("批量删除数据, ids:{}", ids);
        try {
            Integer result = bookService.batchDelete(ids);
            if (result <= 0) {
                return false;
            }
            return true;
        } catch (Exception e) {
            log.error("批量删除数据失败, ids:{}, e:{}", ids, e);
            return false;
        }
    }


    public List<BookInfo> mockBookData() {
            List<BookInfo> bookInfos = new ArrayList<>();
            for (int i = 1; i < 15; i++)
            {           BookInfo bookInfo = new BookInfo();
                bookInfo.setId(i);
                bookInfo.setBookName("书籍" + i);
                bookInfo.setAuthor("作者" + i);
                bookInfo.setCount(i * 5 + 3);
                bookInfo.setPrice(new BigDecimal(new Random().nextInt(100)));
                            bookInfo.setStatus(1);
                            bookInfo.setPublish("出版社"+i);
                            bookInfos.add(bookInfo);         }         return bookInfos;     }
}
